<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/5/29
 * Time: 9:19
 */

namespace App\Http\Controllers\Finance;

use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\LoanBookModel;
use App\Http\Model\LoanModel;
use App\Http\Model\ProjectModel;
use App\Http\Model\YearModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LoanController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function projectLists(Request $request)
    {
        $rules = [
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $loanModel = new LoanModel();
            $loanBookModel = new LoanBookModel();
            $lists = $projectModel->lists($param);
            $countLists = $projectModel->countLists($param);
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d H:i:s'));
            foreach ($lists as $key => $l) {
                $lists[$key]->totalAccount = $loanModel->getSum([
                    'projectId' => $l->id,
                    'startTime'=>$yearInfo['startTime'],
                    'endTime'=>$yearInfo['endTime']
                ]);
                $lists[$key]->totalBook = $loanBookModel->getSum([
                    'projectId' => $l->id,
                    'startTime'=>$yearInfo['startTime'],
                    'endTime'=>$yearInfo['endTime']
                ]);
            }

            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 730101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function bookLists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length', 'page', 'search', 'projectId', 'startTime', 'endTime');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $loanModel = new LoanModel();
            $yearModel = new YearModel();
            $yearInfo = $yearModel->findYear(date('Y-m-d H:i:s'));
            if ($request->startTime == ''){
                $param['startTime'] = $yearInfo['startTime'];
            }
            if ($request->endTime == ''){
                $param['endTime'] = $yearInfo['endTime'];
            }
            $lists = $loanBookModel->lists($param);
            $countLists = $loanBookModel->countLists($param);

            foreach ($lists as $key => $l) {
                $lists[$key]->accountSum = $loanModel->getSum(['bookId'=>$l->id,
                    'startTime'=>$yearInfo['startTime'],
                    'endTime'=>$yearInfo['endTime']]);
            }
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 730201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addBook(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'name' => 'required',
            'account' => 'required|numeric',
//            'startTime' => 'required|date_format:Y-m-d',
//            'endTime' => 'required|date_format:Y-m-d',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'name.required' => '请填写册次名称',
            'account.required' => '请填写册次总金额',
            'account.numeric' => '册次总金额参数类型错误',
            'startTime.required' => '请选择册次周期起始时间',
            'startTime.date_format' => '册次周期起始时间格式不正确',
            'endTime.required' => '请选择册次周期截止时间',
            'endTime.date_format' => '册次周期截止时间格式不正确',
        ];
        $param = $request->only('projectId', 'name', 'account', 'remark');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $param['adminId'] = $request->session()->get('adminId');
            $bookId = $loanBookModel->insert($param);
            if (empty($bookId)) {
                throw new ApiException('保存失败', 730302);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 730301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function bookInfo(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取账簿册次参数失败',
            'id.integer' => '账簿册次参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $this->data = $loanBookModel->info(['id' => $param['id']]);
        } else {
            throw new ApiException($validator->errors()->first(), 730401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteBook(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取账簿册次参数失败',
            'id.integer' => '账簿册次参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $info = $loanModel->info(['bookId' => $param['id']]);
            if (!empty($info)) {
                throw new ApiException('借款册次下已有借款明细不能删除', 730502);
            }
            $loanBookModel = new LoanBookModel();
            $bookInfo = $loanBookModel->info(['id'=>$param['id']]);
            if ($bookInfo['status'] == 3) {
                throw new ApiException('借款册次已经封存，不能删除', 730503);
            }
            $loanBookModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 730501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function accountLists(Request $request)
    {
        $rules = [
            'bookId' => 'nullable|integer',
            'projectId' => 'nullable|integer',
            'professionId' => 'nullable|integer',
            'month' => 'nullable|date_format:Y-m',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'bookId.required' => '获取借款册次参数失败',
            'bookId.integer' => '借款册次参数类型错误',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'month.date_format' => '日期格式类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('bookId', 'length', 'page','projectId','professionId','month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $lists = $loanModel->lists($param);
            $countLists = $loanModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 730601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addAccount(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'bookId' => 'required|integer',
            'employeeId' => 'required|integer',
            'account' => 'required|numeric',
            'loanTime' => 'required|date_format:Y-m-d',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'bookId.required' => '获取项目参数失败',
            'bookId.integer' => '项目参数类型错误',
            'employeeId.required' => '获取工人参数失败',
            'employeeId.integer' => '工人参数类型错误',
            'account.required' => '请填写借支金额',
            'account.numeric' => '借支金额参数类型错误',
            'loanTime.required' => '请填写借支时间',
            'loanTime.date_format' => '借支时间格式不正确',
        ];
        $param = $request->only('employeeId','bookId', 'account', 'loanTime', 'projectId', 'remark');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $info = $loanBookModel->info(['id'=>$param['bookId']]);
            if ($info['status'] == 3){
                throw new ApiException('借款册次'.$info['name'].'已经封账，不能添加借款记录', 730702);
            }
            $loanModel = new LoanModel();
            $param['adminId'] = $request->session()->get('adminId');
            $loanModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 730701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteAccount(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取借款记录参数失败',
            'id.integer' => '借款记录参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $loanModel = new LoanModel();
            $info = $loanModel->info(['id'=>$param['id']]);
            if (!empty($info)){
                $bookInfo = $loanBookModel->info(['id'=>$info['bookId']]);
                if ($bookInfo['status'] == 3 ||$info['status'] == 3){
                    throw new ApiException('借款册次已经封存，不能删除任何借款记录', 730802);
                }
                $loanModel->delete(['id'=>$param['id']]);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 730801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function mothball(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取账簿册次参数失败',
            'id.integer' => '账簿册次参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanBookModel = new LoanBookModel();
            $loanBookModel->update(['id' => $param['id']],['status'=>3]);
            $loanModel = new LoanModel();
            $loanModel->update(['bookId'=>$param['id']],['status'=>3]);
        } else {
            throw new ApiException($validator->errors()->first(), 730901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loanLog(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'month' => 'nullable|date_format:Y-m',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'month.integer' => '月份格式不正确',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length', 'page', 'search', 'projectId', 'professionId', 'startTime', 'endTime', 'month');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $lists = $loanModel->lists($param);
            $countLists = $loanModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 731001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 借款统计汇总
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function loanCollect(Request $request)
    {
        $rules = [
            'projectId' => 'nullable|integer',
            'bookId' => 'nullable|integer',
            'professionId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
        ];
        $message = [
//            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
//            'bookId.required' => '获取借款册次参数失败',
            'bookId.integer' => '借款册次参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'startTime.date_format' => '起始时间格式错误',
            'endTime.date_format' => '截止时间格式错误',
        ];
        $param = $request->only(['projectId', 'bookId', 'professionId', 'startTime', 'endTime', 'search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $loanModel = new LoanModel();
            $this->data['account'] = $loanModel->getSum($param);
        } else {
            throw new ApiException($validator->errors()->first(), 731101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}